package com.softserve.academy.food.services;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.softserve.academy.food.dao.HibernateDish;
import com.softserve.academy.food.dao.HibernateHistoryOrder;
import com.softserve.academy.food.dao.HibernateOrder;
import com.softserve.academy.food.dao.HibernateUser;
import com.softserve.academy.food.entity.eDish;
import com.softserve.academy.food.entity.eHistoryOrders;
import com.softserve.academy.food.entity.eOrder;
import com.softserve.academy.food.services.main.dishService;


@Service
public class orderService extends dishService implements iOrderService
{
	@Autowired
	private HibernateOrder daoOrder;
	@Autowired
	private HibernateUser daoUser;
	@Autowired
	private HibernateHistoryOrder daoHistoryOrder;
	@Autowired
	private HibernateDish daoDish;
	
	@Override
	@Transactional
	public void addOrder( ArrayList<Integer> basked, String login )
	{
		if ( basked!=null )
		{
			if ( login!=null )
			{
				eHistoryOrders histroy = new eHistoryOrders();
				ArrayList<eOrder> orders = new ArrayList<eOrder>();
				
				histroy.setUser( daoUser.getUser(login) );
				histroy.setData( new java.util.Date() );
				histroy = daoHistoryOrder.Create( histroy );
				
				for ( eDish dish : daoDish.getDishesByIds(basked) )
				{
					eOrder order = new eOrder();
					order.setHistory( histroy );
					order.setDish(dish);
					orders.add( daoOrder.Create(order) );
				}
				
				daoHistoryOrder.Update( histroy );
			}
		}
	}

	

}
